﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@using dPet.Application.DataObjects;

@model dPet.Web.Admin.ViewModels.StaffListModel

<div class="clearfix">
	<h4>员工列表 [ @Model.Hospital.Name ]</h4>

	<hr />

	<div class="panel panel-default clearfix">
		<div class="panel-heading">
			<a href="@Url.Action("HospitalList", "Manage")" class="btn btn-sm btn-default"><span class="glyphicon glyphicon-chevron-left"></span> 返 回</a>
			<a href="javascript:;" id="btn_add_staff" class="btn btn-sm btn-success" target="_self"><span class="glyphicon glyphicon-plus-sign"></span> 添加员工</a>
		</div>

		<table class="table text-center table-striped table-hover">
			<thead>
				<tr>
					<th class="text-center">登录邮箱</th>
					<th class="text-center">姓名</th>
					<th class="text-center">联系方式</th>
					<th class="text-center">角色</th>
					<th class="text-center">操作</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var staff in Model.Staffs)
				{
					<tr>
						<td>@staff.LoginName</td>
						<td>@staff.Name</td>
						<td>@staff.Contact</td>
						<td>@staff.StaffRole.Name</td>
						<td>
							@Html.ActionLink("编辑", "Staff", new { id = staff.Id, hospitalId = Model.Hospital.Id })
							&nbsp;&nbsp;
							<a href="javascript:;" onclick="change_password('@staff.Id')">修改密码</a>
						</td>
					</tr>
				}
			</tbody>
		</table>
	</div>
</div>

<div class="modal fade" id="staff_modal" tabindex="-1" role="dialog" aria-labelledby="staff_modal_label" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">添加新员工</h4>
			</div>

			<div class="modal-body form-horizontal">
				@using (Html.BeginForm("SaveStaff", "Manage", FormMethod.Post, new { id = "save_staff_form" }))
				{
					<input type="hidden" name="HospitalId" value="@Model.Hospital.Id" />

					<div class="form-group">
						<label for="StaffRole" class="col-md-3 control-label">角　　色</label>
						<div class="col-md-4">
							@Html.DropDownList("StaffRole.Id", new SelectList(Model.StaffRoles as IEnumerable<StaffRoleDto>, "Id", "Name"), new { @class = "form-control" })
						</div>
					</div>

					<div class="form-group">
						<label for="txt_logingname" class="col-md-3 control-label">登录邮箱</label>
						<div class="col-md-8">
							<input id="txt_logingname" name="LoginName" type="text" class="form-control" placeholder="请输入登录邮箱" />
						</div>
					</div>

					<div class="form-group">
						<label for="txt_password" class="col-md-3 control-label">密　　码</label>
						<div class="col-md-8">
							<input id="txt_password" name="Password" type="text" class="form-control" placeholder="请输入密码" />
						</div>
					</div>

					<div class="form-group">
						<label for="txt_name" class="col-md-3 control-label">姓　　名</label>
						<div class="col-md-8">
							<input id="txt_name" name="Name" type="text" class="form-control" value="" placeholder="请输入姓名" />
						</div>
					</div>

					<div class="form-group">
						<label for="txt_contact" class="col-md-3 control-label">联系方式</label>
						<div class="col-md-8">
							<input id="txt_contact" name="Contact" type="text" class="form-control" value="" placeholder="请输入联系方式" />
						</div>
					</div>
				}
			</div>
			<div class="modal-footer">
				<button id="btn_add_staff_submit" type="button" class="btn btn-default">添 加</button>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="change_password_modal" tabindex="-1" role="dialog" aria-labelledby="change_password_modal_label" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">修改密码</h4>
			</div>

			<div class="modal-body form-horizontal">
				<input id="hidden_staff_id" type="hidden" value="" />

				<div class="form-group">
					<label for="txt_new_password" class="col-md-3 control-label">新密码</label>
					<div class="col-md-8">
						<input id="txt_new_password" type="text" class="form-control" placeholder="请输入新密码" />
					</div>
				</div>
			</div>

			<div class="modal-footer">
				<button id="btn_change_password_submit" type="button" class="btn btn-default">确 定</button>
			</div>
		</div>
	</div>
</div>

@section scripts {
	<script type="text/javascript">
		$("#btn_add_staff").bind("click", function () {
			$('#staff_modal').modal('show');
		});

		$("#btn_add_staff_submit").bind("click", function () {
			if ($.trim($("#txt_logingname").val()) == '') {
				alert("请输入登录邮箱");
				return false;
			}

			if ($.trim($("#txt_password").val()) == '') {
				alert("请输入密码");
				return false;
			}

			$('#save_staff_form').submit();
		});

		function change_password(staffId) {
			$("#hidden_staff_id").val(staffId);
			$("#txt_new_password").val('');

			$('#change_password_modal').modal('show');
		}

		$("#btn_change_password_submit").bind("click", function () {
			var staffId = $("#hidden_staff_id").val();
			var newPassword = $("#txt_new_password").val();

			if (newPassword == '') {
				alert("请输入新密码");
				return false;
			}

			$.ajax({
				type: "POST",
				dataType: 'json',
				url: "/Manage/ResetPassword",
				data: {
					'staffId': staffId,
					'newPassword': newPassword
				},
				success: function (r, status) {
					alert(r.Message);

					if (r.Success) {
						$('#change_password_modal').modal('hide');
					}
				}
			});

			$("#hidden_staff_id").val('');
			$("#txt_new_password").val('');
		});
	</script>
}
